package me.zhengjie.spider.util.processor;

import lombok.Data;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.comic.ComicBean;
import me.zhengjie.spider.domain.comic.ComicRuleBean;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

@Data
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpiderComicDetailPageProcessor implements PageProcessor {

    private ComicBean comicBean;
    public ComicRuleBean ruleBean;

    private Site site = Site.me().setRetryTimes(10).setSleepTime(1000).setTimeOut(30000)
            .addHeader("Accept-Encoding", "/").setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36");

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        page.putField(SpiderConstant.AUTHOR, page.getHtml().xpath(this.ruleBean.getAuthor()).all());
        page.putField(SpiderConstant.TAG, page.getHtml().xpath(this.ruleBean.getTag()).all());
        page.putField(SpiderConstant.DETAIL_HREF, page.getHtml().xpath(this.ruleBean.getDetailHref()).toString());
    }
}
